﻿using System.Collections.Generic;
using System.Linq;
using DynamicChart.Service.FunctionConversion;
using DynamicChart.Util;

namespace DynamicChart.Service.Step.GenerateSQL;

public class GenerateSortStep : GenerateAbstractSQLStep
{
    public GenerateSortStep(ISqlFunctionAdapter adapter) : base(adapter)
    {
    }
    
    public override void Process(IDictionary<string, object> data, IDictionary<string, string> res)
    {
        var sortFields = DataProcessor.EnumerateFields(data)
            .Where(t => t.Entity.ContainsKey("Sort") && t.Entity.ContainsKey("ScreenMode"))
            .Select(t =>
            {
                var field = CreateField(t.Entity, $"{t.TableKey}.{t.ColumnKey}", "ScreenMode");
                return $"{field} {t.Entity["Sort"]}";
            })
            .ToList();

        res["sort"] = string.Join(", ", sortFields);
    }
}